#include "StdAfx.h"
#include "cQuadVertexObj.h"

struct QUAD_VERTEX
{
	D3DXVECTOR3	pos;
	D3DCOLOR			color;
	FLOAT					tu, tv;
	
	enum{ FVF = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1 };
};
cQuadVertexObj::cQuadVertexObj(void)
{
	mScale = 10.f;
	mRotation = D3DXVECTOR3( D3DX_PI * 90/180, 0.f, 0.f );
}

cQuadVertexObj::~cQuadVertexObj(void)
{
}

// void cQuadVertexObj::Init( const char* szFileName, D3DCOLOR color )
// {
// 	assert( NULL != szFileName );
// 
// 	HR(::D3DXCreateTextureFromFileEx( gd3dDevice,
// 			szFileName,
// 			D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
// 			D3DX_DEFAULT,
// 			0,
// 			D3DFMT_UNKNOWN,
// 			D3DPOOL_MANAGED,
// 			D3DX_FILTER_TRIANGLE|D3DX_FILTER_MIRROR,
// 			D3DX_FILTER_TRIANGLE|D3DX_FILTER_MIRROR,
// 			color,
// 			NULL,
// 			NULL, 
// 			&mpTexture) );
// 
// 	QUAD_VERTEX vertices[] = {
// 			{ D3DXVECTOR3( -0.5f, -0.5f, 0.f), WHITE, 0.f, 0.f	 },
// 			{ D3DXVECTOR3(  0.5f, -0.5f, 0.f), WHITE, 1.f, 0.f	 },
// 			{ D3DXVECTOR3(  -0.5f,  0.5f, 0.f), WHITE, 0.f, 1.f },
// 			{ D3DXVECTOR3(  0.5f, 0.5f,  0.f), WHITE, 0.f, 1.f },
// 		};
// 
// 	HR( gd3dDevice->CreateVertexBuffer( sizeof( vertices ),
// 		0, QUAD_VERTEX::FVF, D3DPOOL_MANAGED,
// 		&mQuadVB, NULL ) );
// 
// 	VOID* pVertices = NULL;
// 	HR( mQuadVB->Lock( 0, 0, (VOID**)&pVertices, 0 ) );
// 	memcpy( pVertices, vertices, sizeof( vertices ) );
// 
// 	mQuadVB->Unlock();
// }



void cQuadVertexObj::Init( LPDIRECT3DTEXTURE9 tex, FLOAT x, FLOAT y, FLOAT w, FLOAT h )
{
	mpTexture = tex;
	mpTexture->AddRef();

	QUAD_VERTEX vertices[] = {
		{ D3DXVECTOR3( -0.5f, -0.5f, 0.f ), WHITE, x, y},
		{ D3DXVECTOR3(  0.5f, -0.5f, 0.f ), WHITE, x + w, y},
		{ D3DXVECTOR3( -0.5f, 0.5f, 0.f ), WHITE, x, y + h },
		{ D3DXVECTOR3( 0.5f,  0.5f, 0.f ), WHITE, x + w, y + h }
	};

	HR( gd3dDevice->CreateVertexBuffer( sizeof( vertices ),
		0, QUAD_VERTEX::FVF, D3DPOOL_MANAGED,
		&mQuadVB, NULL ) );

	VOID* pVertices = NULL;
	HR( mQuadVB->Lock( 0, 0, (VOID**)&pVertices, 0 ) );
	memcpy( pVertices, vertices, sizeof( vertices ) );

	mQuadVB->Unlock( );
}
void cQuadVertexObj::Release()
{
	ReleaseCOM( mQuadVB );
	ReleaseCOM( mpTexture );
}

void cQuadVertexObj::PreRender()
{
	gd3dDevice->GetRenderState( D3DRS_CULLMODE, &mOldCullMode );
	gd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );

	gd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE);
	gd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );

	gd3dDevice->SetRenderState( D3DRS_ALPHAREF, 0x08 );
	gd3dDevice->SetRenderState( D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL );
}

void cQuadVertexObj::PostRender()
{
	gd3dDevice->SetRenderState( D3DRS_CULLMODE, mOldCullMode );
	gd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
	//gd3dDevice->SetRenderState( D3DRS_ALPHATESTENABLE, FALSE );
}
void cQuadVertexObj::Draw( FLOAT x, FLOAT y, FLOAT z )
{
	D3DXMATRIX matWorld, matScale, matRotation;

	D3DXMatrixScaling( &matScale, mScale, mScale, mScale );
	D3DXMatrixRotationYawPitchRoll( &matRotation, mRotation.y, mRotation.x, mRotation.z );

	matWorld = matScale * matRotation;
	matWorld._41 = x;
	matWorld._42 = y;
	matWorld._43 = z;

	gd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );

	PostRender();

	gd3dDevice->SetTexture( 0, mpTexture );
	gd3dDevice->SetStreamSource( 0, mQuadVB, 0, sizeof( QUAD_VERTEX ) );
	gd3dDevice->SetFVF( QUAD_VERTEX::FVF );
	gd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
	
	//PreRender();
}
